package com.zzw.utils;

import org.springframework.http.server.reactive.ServerHttpRequest;

import java.util.List;

/**
 * @author liuyulong
 */
public class IpUtils {

    public static String tryGetRealIp(ServerHttpRequest request) {
        List<String> xForwardedFor = request.getHeaders().get("X-Forwarded-For");
        if (xForwardedFor != null && !xForwardedFor.isEmpty()) {
            String firstForwardedIp = xForwardedFor.get(0);
            return firstForwardedIp.split(",")[0].trim();
        }
        String xRealIp = request.getHeaders().getFirst("X-Real-IP");
        if (xRealIp != null) {
            return xRealIp;
        }
        return request.getRemoteAddress().getAddress().getHostAddress();
    }
}
